package com.sanjiahantang.youjiarole.module.common.views.impl;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.common.beans.DataBean;
import com.sanjiahantang.youjiarole.module.common.beans.StringBean;
import com.sanjiahantang.youjiarole.module.common.beans.UserOBean;
import com.sanjiahantang.youjiarole.module.common.presenters.MeAccountPresenter;
import com.sanjiahantang.youjiarole.module.common.views.MeAccountView;
import com.squareup.picasso.Picasso;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 我的账户明细
 * Created by admin on 2017/10/28.
 */

public class MeAccountDetailActivity extends BaseMvpActivity<MeAccountView, MeAccountPresenter> implements MeAccountView, View.OnClickListener {
    private ListView detail_list, optionList;
    private QuickAdapter<UserOBean.Data> adapter;
    private View fl_Left, llMask,panelType;
    private TextView title, txtType, txtTotal;
    private String orderCode;
    private int currentType;
    private final int[] ICON_ARR = new int[]{
            R.drawable.i_income_1,
            R.drawable.i_income_3,
            R.drawable.i_income_4,
            R.drawable.i_income_2,
            R.drawable.i_income_5,
    };


    @Override
    public MeAccountPresenter initPresenter() {
        return new MeAccountPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_me_account_detail);
        findViewById(R.id.panelOption).setVisibility(View.VISIBLE);
        detail_list = (ListView) findViewById(R.id.detail_list);
        optionList = (ListView) findViewById(R.id.optionList);
        fl_Left = findViewById(R.id.fl_Left);
        fl_Left.setOnClickListener(this);
        title = (TextView) findViewById(R.id.title);

        txtType = (TextView) findViewById(R.id.txtType);
        txtTotal = (TextView) findViewById(R.id.txtTotal);
        llMask = findViewById(R.id.llMask);
        panelType = findViewById(R.id.panelType);
        panelType.setOnClickListener(this);
        findViewById(R.id.txtSearch).setOnClickListener(this);

        title.setText("明细");

        ArrayList<String> typeNames = new ArrayList<>();
        typeNames.add("全部");
        typeNames.add("引客收入");
        typeNames.add("设计收入");
        typeNames.add("施工收入");
        typeNames.add("监理收入");
        typeNames.add("提现支出");

        optionList.setAdapter(new QuickAdapter<String>($this, R.layout.item_text,typeNames) {
            @Override
            protected void convert(final BaseAdapterHelper helper, final String item) {
                helper.setText(R.id.text1,item);
                helper.getView().setBackgroundResource(R.color.white);
                helper.setVisible(R.id.ivArcher,false);
                helper.getView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        currentType = helper.getPosition();
                        txtType.setText(item);
                        initData();
                    }
                });
            }
        });
    }

    @Override
    public void initData() {

        presenter.WithdrawalsMoneyDetail($this, currentType, orderCode, new OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                Object[] param = (Object[]) content;
                switch ((int) param[0]) {
                    case ACCOUNT_TYPE_COMMEN:
                        final SimpleDateFormat oldFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        final SimpleDateFormat newFormatter = new SimpleDateFormat("MM-dd HH:mm");
                        adapter = new QuickAdapter<UserOBean.Data>($this, R.layout.item_me_account_detail, (List<UserOBean.Data>) param[1]) {
                            @Override
                            protected void convert(BaseAdapterHelper helper, UserOBean.Data item) {
                                ImageView imageView = (ImageView) helper.getView().findViewById(R.id.iv_image);

                                String time = item.getCreateTime();
                                time = time.replaceAll("[a-zA-Z]", "  ").substring(0, time.indexOf(".") + 1);
                                try {
                                    Date date = oldFormatter.parse(time);
                                    time = newFormatter.format(date);
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
                                helper.setText(R.id.txtCreateTime, time);
                                if (!TextUtils.isEmpty(item.getOrderCode())) {
                                    helper.setText(R.id.txtOrderCode, "订单号: " + item.getOrderCode());
                                } else {
                                    helper.setText(R.id.txtOrderCode, "");
                                }

                                if (!TextUtils.isEmpty(item.getSubcontractPhone())) {
                                    helper.setText(R.id.txtSubcontract, "分包: " + item.getSubcontractName() + "(" + item.getSubcontractPhone() + ")");
                                } else {
                                    helper.setText(R.id.txtSubcontract, "");
                                }

                                String action = "";
                                String money = "";
                                int incomeType = item.getUserRole() - 1;
                                switch (item.getUserRole()) {
                                    case 1:
                                        action = "引客收入 " + (TextUtils.isEmpty(item.getEngnieeName()) ? "" : item.getEngnieeName());
                                        money = "+ " + item.getMoney();
                                        break;
                                    case 2:
                                        action = "施工收入 " + item.getEngnieeName();
                                        money = "+ " + item.getMoney();
                                        break;
                                    case 3:
                                        action = "监理收入 " + item.getEngnieeName();
                                        money = "+ " + item.getMoney();
                                        break;
                                    case 4:
                                        action = "设计收入 " + item.getEngnieeName();
                                        money = "+ " + item.getMoney();
                                        break;
                                    default:
                                        incomeType = 4;
                                        action = "提现支出";
                                        money = "- " + item.getMoney();
                                        break;

                                }

                                imageView.setImageResource(ICON_ARR[incomeType]);
                                helper.setText(R.id.txtMoney, money);
                                helper.setText(R.id.txtName, action);
                            }
                        };
                        detail_list.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                        detail_list.setSelector(R.color.transparent);
                        break;

                }
            }
        });

        presenter.findMoneyRecordNumberInfo($this, currentType, orderCode, new OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                if (content != null && content instanceof DataBean.Data) {
                    DataBean.Data money = (DataBean.Data) content;
                    txtTotal.setText("共计：¥" + money.getMoney());
                } else {
                    txtTotal.setText("共计：¥0");
                }
            }
        });

        llMask.setVisibility(View.GONE);
        optionList.setVisibility(View.GONE);
    }

    @Override
    public void showProgress() {
        try {
            super.showProgress($this);
        }catch (Exception e){

        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
            case R.id.panelType:
                optionList.setVisibility(View.VISIBLE);
                llMask.setVisibility(View.VISIBLE);
                break;
            case R.id.txtSearch:
                Intent intent = new Intent($this, SearchActivity.class);
                intent.putExtra(KEY_TYPE,SEARCH_TYPE_ACCOUNT);
                startActivityForResult(intent,INTENT_SEARCH);
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == INTENT_SEARCH && resultCode == RESULT_OK){
            orderCode = data.getStringExtra(KEY_KEYWORDS);
            initData();
        }
    }


}
